package cn.bdqn.t354.service.impl;


import cn.bdqn.t354.mapper.CstServiceMapper;
import cn.bdqn.t354.pojo.CstService;
import cn.bdqn.t354.pojo.Customer;
import cn.bdqn.t354.service.CstServiceService;
import cn.bdqn.t354.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CstServiceServiceImpl implements CstServiceService {
    @Resource
    private CstServiceMapper cstServiceMapper;
    @Resource
    private CustomerService customerService;

    @Override
    public void addCstService(CstService cstService) {
        //设置创建时间为当前时间
        cstService.setSvrCreateDate(new Date());
        //刚创建的服务为新创建状态
        cstService.setSvrStatus("新创建");

        //设置客户名称
        Customer customer=customerService.getCustomer(cstService.getSvrCustNo());
        cstService.setSvrCustName(customer.getCustName());

        cstServiceMapper.insert(cstService);
    }

    @Override
    public IPage<CstService> getCstServicesByPage(String custName, String title, String type,
                             String status, Integer pageNo, Integer pageSize) {
        //组装分页条件
        Page<CstService> page=new Page<>(pageNo, pageSize);

        //组装过滤条件 动态sql
        QueryWrapper<CstService> qw=new QueryWrapper();
        qw.eq("svr_status",status);
        qw.like(custName!=null&&!"".equals(custName),"svr_cust_name",custName);
        qw.like(title!=null&&!"".equals(title),"svr_title",title);
        qw.eq(type!=null&&!"".equals(type),"svr_type",type);

        //调用分页方法
        IPage<CstService> cstServicePage=cstServiceMapper.selectPage(page, qw);
        //返回分页对象
        return cstServicePage;
    }

    @Override
    public CstService getCstService(Long id) {
        return cstServiceMapper.selectById(id);
    }

    @Override
    public void updateCstService(CstService cstService) {
        cstServiceMapper.updateById(cstService);
    }

    @Override
    public void deleteCstService(Long id) {
         cstServiceMapper.deleteById(id);
    }

}
